<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <!--      <el-form-item label="用户类型" prop="userType">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.userType"-->
      <!--          placeholder="请选择用户类型"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="dict in common_user_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="操作人" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择操作人员"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in userList"
            :key="item.staffId!"
            :label="item.nickname"
            :value="item.staffId!"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作模块" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择操作模块"
          filterable
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in moduleList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.type"-->
<!--          placeholder="请输入操作模块"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="操作名" prop="subType">
        <el-input
          v-model="queryParams.subType"
          placeholder="请输入操作名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="业务数据编号" prop="bizId">
        <el-input
          v-model="queryParams.bizId"
          placeholder="请输入业务数据编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="操作IP" prop="userIp">-->
<!--        <el-input-->
<!--          v-model="queryParams.userIp"-->
<!--          placeholder="请输入操作IP"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="链路追踪编号" prop="traceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.traceId"-->
<!--          placeholder="请输入链路追踪编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="操作时间" prop="operateTime">
        <el-date-picker
          v-model="queryParams.operateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      :tooltip-options="{ popperClass: 'op-log-popper' }"
    >
      <el-table-column label="日志编号" prop="id" width="120" />
      <el-table-column label="操作人" prop="userName" width="130" />
      <el-table-column label="操作模块" prop="type" width="140" />
      <el-table-column label="操作名" prop="subType" width="140" />
      <el-table-column label="操作内容" prop="action" />
      <el-table-column label="业务数据编号" prop="bizId" width="130" />
      <!--      <el-table-column label="拓展字段" prop="extra" />-->
      <!--      <el-table-column label="请求地址" prop="requestUrl" />-->
      <!--      <el-table-column label="请求方法" prop="requestMethod" />-->
      <!--      <el-table-column label="用户类型" prop="userType">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :options="common_user_type" :value="scope.row.userType" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作IP" prop="userIp" width="140" />
      <!--      <el-table-column label="浏览器UA" prop="userAgent" />-->
      <!--      <el-table-column label="链路追踪编号" prop="traceId" />-->
      <el-table-column label="操作时间" prop="operateTime" width="180px" />
      <el-table-column label="操作" width="80px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:operate-log:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <OperateLogDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { OperateLogApi, OperateLogVO } from '@/api/system/operate_log'
import OperateLogDetail from './OperateLogDetail.vue'
import * as StaffApi from '@/api/system/staff'
import * as DictDataApi from '@/api/system/dict/dict_data'

/** 用户操作日志 列表 */
defineOptions({ name: 'SystemOperateLog' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const listLoading = ref(true) // 列表的加载中状态
const list = ref<OperateLogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  type: undefined,
  subType: undefined,
  bizId: undefined,
  userId: undefined,
  userType: undefined,
  userIp: undefined,
  traceId: undefined,
  operateTime: [],
  pageNo: 1,
  pageSize: 20
})
const queryFormRef = ref() // 搜索的表单
const userList = ref()
const moduleList = ref()

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await OperateLogApi.getOperateLogPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 详情 */
const detailRef = ref<InstanceType<typeof OperateLogDetail>>()
const openDetail = (data: OperateLogVO) => {
  detailRef.value?.open(data)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  userList.value = await StaffApi.getStaffSimpleList()
  moduleList.value = await DictDataApi.getDictDataSimpleList(DICT_TYPE.system_operate_log_module)
})
</script>

<style scoped lang="scss">
:deep(.op-log-popper) {
  max-width: 700px;
}
</style>
